package net.jeeshop.web.action.front.indexnews;

import net.jeeshop.core.dao.page.PagerModel;
import net.jeeshop.core.front.SystemManager;
import net.jeeshop.services.front.catalog.bean.Catalog;
import net.jeeshop.services.front.indexnews.IndexNewsService;
import net.jeeshop.services.front.indexnews.bean.IndexNews;
import net.jeeshop.services.front.news.bean.News;
import net.jeeshop.web.action.front.FrontBaseController;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 文章管理
 * 
 * @author huangf
 * 
 */
@Controller("frontIndexNewsAction")
public class IndexNewsAction extends FrontBaseController<IndexNews> {

	private static final long serialVersionUID = 1L;
	private static final Logger logger = LoggerFactory
			.getLogger(IndexNewsAction.class);

	@Autowired
	private IndexNewsService indexNewsService;

	@Override
	public IndexNewsService getService() {
		return indexNewsService;
	}

	@ModelAttribute("indexnewsCatalogs")
	public List<Catalog> getNewsCatalogs(){
		return SystemManager.getInstance().getNewsCatalogs();
	}

	@RequestMapping("indexnews/list")
	public String newsList(ModelMap model, IndexNews e) throws Exception{
		//新闻
		e.setType("notice");
		PagerModel pager = selectPageList(getService(), e);
		pager.setPagerUrl("list");
		List lists = pager.getList();
		if (lists.size() >= 7){
			List list = lists.subList(0, 6);
			pager.setList(list);
		}
		model.addAttribute("pager", pager);
		model.size();
		//公益项目
		e.setType("public");
		PagerModel pagerPublic = selectPageList(getService(), e);
		pagerPublic.setPagerUrl("list");
		List listsPublic = pagerPublic.getList();
		if (listsPublic.size() >= 7){
			List list = listsPublic.subList(0, 6);
			pagerPublic.setList(list);
		}
		model.addAttribute("pagerPublic", pagerPublic);
		model.size();
		return "newindex/index";
	}

	@RequestMapping("indexnews/newlist")
	public String newlist(ModelMap model, IndexNews e) throws Exception{
		e.setType("notice");
		PagerModel pager = selectPageList(getService(), e);
		pager.setPagerUrl("list");
		model.addAttribute("pager", pager);
		model.size();
		return "newindex/newlist";
	}

	@RequestMapping("indexnews/newlistPublic")
	public String newlistPublic(ModelMap model, IndexNews e) throws Exception{
		e.setType("public");
		PagerModel pager = selectPageList(getService(), e);
		pager.setPagerUrl("list");
		model.addAttribute("pager", pager);
		model.size();
		return "newindex/newlistpublic";
	}

	/**
	 * 获取新闻详情
	 * @return
	 */
	@RequestMapping("/indexnews/info/{id}")
	public String newsInfo(@PathVariable("id")String id, ModelMap model) throws Exception{
		logger.error("NewsAction.newsInfo=== id="+id);
		if(StringUtils.isBlank(id)){
			throw new NullPointerException("id is null");
		}
		IndexNews news =indexNewsService.selectById(id);
		if(news==null){
			throw new NullPointerException();
		}
		String url = "/jsp/notices/"+news.getId()+".jsp";
		logger.error("url = " + url);
		model.addAttribute("newsInfoUrl", url);
		model.addAttribute("news", news);
		return "newindex/newsdetail";
	}

	@RequestMapping("/indexnews/infopublic/{id}")
	public String newsInfopublic(@PathVariable("id")String id, ModelMap model) throws Exception{
		logger.error("NewsAction.newsInfo=== id="+id);
		if(StringUtils.isBlank(id)){
			throw new NullPointerException("id is null");
		}
		IndexNews news =indexNewsService.selectById(id);
		if(news==null){
			throw new NullPointerException();
		}
		String url = "/jsp/notices/"+news.getId()+".jsp";
		logger.error("url = " + url);
		model.addAttribute("newsInfoUrl", url);
		model.addAttribute("news", news);
		return "newindex/newsdetailpublic";
	}
}
